﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.CommonDao;
using CSP.Bll.IObjectBlls;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Common.Factories;
using CSP.Common;
using CSP.Dao;
using CSP.Bll.CommonBll;
using CSP.Model.CSP;
using CSP.Model.CSPEnum;

namespace CSP.Bll.ObjectBlls
{
    public class RecommendBll : CSPCommonBll, IRecommendBll
    {
        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEY = "Recommend";

        private IRecommendDao _recommendDao;

        private ISearchServiceListDao _searchServiceListDao;

        public RecommendBll()
        {
            _cacheProvider = CachedProviderFactory.GetLocalCachedProvider();
            _recommendDao = CSPDaoFactory.GetInstance().GetRecommendDao();
            _searchServiceListDao = CSPDaoFactory.GetInstance().GetSearchServiceListDao();
        }

        public Model.CSP.Recommend GetDataById(int id)
        {
            throw new NotImplementedException();
        }

        public ICollection<Model.CSP.Recommend> GeDatatByIds(int[] Ids)
        {
            throw new NotImplementedException();
        }

        public void Add(Model.CSP.Recommend obj)
        {
            throw new NotImplementedException();
        }

        public void Update(Model.CSP.Recommend obj)
        {
            throw new NotImplementedException();
        }

        public void DeleteById(int id)
        {
            throw new NotImplementedException();
        }

        public void RefreshCache()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="recommendType"></param>
        /// <param name="count"></param>
        /// <param name="cacheSeed"></param>
        /// <returns></returns>
        public SearchServiceList[] GetSearchServiceListByRecommendTypeAndCount(Model.CSPEnum.RecommendType recommendType, int count,string cacheSeed="")
        {
            string cacheKey = CACHEKEY +cacheSeed+ recommendType.ToString();
            SearchServiceList[] serviceInfos = _cacheProvider.GetData<SearchServiceList[]>(cacheKey);

            if (serviceInfos == null)
            {
                Recommend[] recommends = null;

                List<int> serviceIds = null;
                switch (recommendType)
                {
                    case RecommendType.RecommendRelated:
                        serviceIds = _searchServiceListDao.GetRandomServiceInfoIdsByCount(count);
                        break;
                    case RecommendType.NoDataRecommendRelated:
                        serviceIds = _searchServiceListDao.GetRandomServiceInfoIdsByCount(count);
                        break;
                    default:
                        recommends = _recommendDao.GetRecommendByRecommendType(recommendType);
                        serviceIds = GetServiceInfoIdsByRecommendsAndCount(recommends, count);
                        break;
                }
                
                serviceInfos = _searchServiceListDao.GetDataByIdsDefault(serviceIds, 0, count).ToArray();
                if (serviceInfos.Length!=0)
                {
                    _cacheProvider.AddData(cacheKey, serviceInfos, DateTime.Now.AddHours(6));    
                }
            }

            return serviceInfos;

        }


        /// <summary>
        /// 根据推荐数据和推荐条数筛选出推荐的Ids
        /// </summary>
        /// <param name="recommends"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private List<int> GetServiceInfoIdsByRecommendsAndCount(Recommend[] recommends, int count)
        {
            List<int> result=new List<int>();
            int temCount=count;

            //取出固定的值
            IEnumerable<Recommend> recommendsFix=recommends.Where(r => r.ShowType == Convert.ToInt32(RecommendShowType.Fix));
            int recommendsFixCount = recommendsFix.Count();
            if (recommendsFix.Count()!= 0)
            {
                foreach(Recommend recommend in recommendsFix)
                {
                    result.Add(recommend.ServiceId);
                }
                temCount = temCount - recommendsFixCount;
            }

            if(temCount<=0)
            {
                return result;
            }

            //取出随机值
            IEnumerable<Recommend> recommendsRandom=recommends.Where(r => r.ShowType == Convert.ToInt32(RecommendShowType.Random));

            if (recommendsRandom.Count()!=0)
            {
                List<int> randomResult = new List<int>();
                List<int> randomServiceIds = recommendsRandom.Select(r => r.ServiceId).ToList();
                Random rd = new Random();
                for (int i = randomServiceIds.Count; i > 0;i-- )
                {
                    if(randomResult.Count==temCount)
                    {
                        break;
                    }
                    int index = rd.Next(0, randomServiceIds.Count - 1);
                    randomResult.Add(randomServiceIds[index]);
                    randomServiceIds.RemoveAt(index);
                }
                result.AddRange(randomResult);
            }

            return result;

        }
    }
}
